package com.eastjet.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.equipment.mapper.DevicePmcsMapper;
import com.eastjet.equipment.mapper.DeviceSpotcheckMapper;
import com.eastjet.equipment.service.DeviceSpotcheckService;
import com.eastjet.equipment.util.PageUtil;
import com.eastjet.equipment.util.RedisUtil;
import com.eastjet.maintenance.domain.DeviceInspectiontask;
import com.eastjet.maintenance.domain.DevicePatrolscheme;
import com.eastjet.maintenance.domain.DevicePmcs;
import com.eastjet.maintenance.domain.DeviceSpotcheck;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @author 杨博文
* @description 针对表【device_spotcheck(点检计划表)】的数据库操作Service实现
* @createDate 2024-11-25 09:23:38
*/
@Service
public class DeviceSpotcheckServiceImpl extends ServiceImpl<DeviceSpotcheckMapper, DeviceSpotcheck>
    implements DeviceSpotcheckService {

    @Autowired
    private  DeviceSpotcheckMapper deviceSpotcheckMapper;

    @Autowired
    private DevicePmcsMapper devicePmcsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public Page<DeviceSpotcheck> schemeList(int page, int size, String condition, String type,String name) {
        // 定义Redis中的键
        String redisKey = "SpotplanList:"+type+name;
        System.out.println("SchemeList-condition:"+condition);
        System.out.println("SchemeList-type:"+type);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("schemeList-从Redis中获取数据");
            List<DeviceSpotcheck> schemeList;
            if (condition != null  && !condition.isEmpty() && condition !="") {
                System.out.println(2);
                schemeList = deviceSpotcheckMapper.schemeList(condition,type,name); // 获取所有数据
            } else if(name != null  && !name.isEmpty() && name !=""){
                System.out.println(2);
                schemeList = deviceSpotcheckMapper.schemeList(condition,type,name); // 获取所有数据
            }else {
                System.out.println(3);
                schemeList   = (List<DeviceSpotcheck>) redisUtil.get(redisKey);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(schemeList, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询DeviceSpotcheck");
            List<DeviceSpotcheck> schemeList;
            // 根据condition条件查询
            if (condition != null && !condition.isEmpty() && condition !="") {
                System.out.println(5);
                schemeList =deviceSpotcheckMapper.schemeList(condition,type,name); // 获取所有数据
            } else if(name != null && !name.isEmpty() && name !=""){
                System.out.println(5);
                schemeList =deviceSpotcheckMapper.schemeList(condition,type,name);
                redisUtil.set(redisKey, schemeList);// 获取所有数据
            }else {
                System.out.println(6);
                condition="";
                schemeList = deviceSpotcheckMapper.schemeList(condition,type,name); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, schemeList);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(schemeList, page, size);
        }
    }

    @Override
    public String addSpotcheck(DeviceSpotcheck deviceSpotcheck) {
        System.out.println("deviceSpotcheck:" + deviceSpotcheck.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库
            if (deviceSpotcheckMapper.insert(deviceSpotcheck) > 0) {
                // 定义Redis的键
                String redisKey = "SpotplanList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    String type="";
                    List<DeviceSpotcheck> deviceSpotcheckList = deviceSpotcheckMapper.schemeList(condition,type,""); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceSpotcheckList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }
    }

    @Override
    public String update(DeviceSpotcheck deviceSpotcheck) {
        System.out.println("deviceSpotcheck:" + deviceSpotcheck.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库
            UpdateWrapper uw=new UpdateWrapper();
            uw.set("update_person",deviceSpotcheck.getUpdatePerson());
            uw.set("spotCheck_untime",deviceSpotcheck.getSpotcheckUntime());
            uw.set("update_time",new Date());
            uw.set("cause",deviceSpotcheck.getCause());
            uw.set("spotcheck_class",deviceSpotcheck.getSpotcheckClassId());
            uw.set("spotcheck_person",deviceSpotcheck.getSpotcheckPerson());
            uw.eq("code",deviceSpotcheck.getCode());
            if (deviceSpotcheckMapper.update(deviceSpotcheck,uw) > 0) {
                // 定义Redis的键
                String redisKey = "SpotplanList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    String type="";
                    List<DeviceSpotcheck> deviceSpotcheckList = deviceSpotcheckMapper.schemeList(condition,type,""); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceSpotcheckList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }

    @Override
    public String del(Integer id,String code) {
        UpdateWrapper uw = new UpdateWrapper();
        uw.set("is_delete", 1);
        uw.eq("spotCheck_id", id);
        String redisKey = "SpotplanList:";
        if (redisUtil.hasKey(redisKey)) {
            String redisKey1 = "PmcsList:";
            UpdateWrapper uw1 = new UpdateWrapper();
            uw1.set("is_delete", 1);
            uw1.eq("code", code);
            devicePmcsMapper.update(new DevicePmcs(),uw1);
            if (redisUtil.hasKey(redisKey1)) {
                Set keys = redisTemplate.keys(redisKey1);
                redisTemplate.delete(keys);
            }

            Set keys = redisTemplate.keys(redisKey);
            redisTemplate.delete(keys);
        }
        return deviceSpotcheckMapper.update(new DeviceSpotcheck(),uw)==1?"删除成功":"删除失败";
    }
}




